/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
 *
 * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
 * This MapVision software dynamically links to unmodified Qt5 Library.
 *
 * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
 * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
 * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
 * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * As an additional permission to the GNU Lesser General Public License version 2.1, the object
 * code form of a “work that uses the Library” may incorporate material from a header file that is
 * part of the Library. You may distribute such object code under terms of your choice,
 * provided that: (i) the header files of the Library have not been modified; and (ii)
 * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
 * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
 * GNU Lesser General Public License version 2.1.
 *
 * Moreover, you may apply this exception to a modified version of the Library, provided that such
 * modification does not involve copying material from the Library into the modified Library’s header
 * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
 * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
 *
 * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this package;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

 * The source code for Qt 5.X SDK is available from Qt Project here:
 * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
 *
 * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
 *
 * Author:       Filip Zavadil
 * Description:  Software for displaying and exporting map tiles.
 * The sources and executables of MapVision software are available:
 *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
 *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
 * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
 *
 * *********************************************************************************************************
*/

#include "dockwidgetaddmapfeatures.h"
#include "ui_dockwidgetaddmapfeatures.h"

DockWidgetAddMapFeatures::DockWidgetAddMapFeatures(MapVision *parent) :
    DockWidgetGeneral(parent), ui(new Ui::DockWidgetAddMapFeatures)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("Map Features"));

    items = new QList<QGraphicsItem*>;

    fillList();
}

DockWidgetAddMapFeatures::~DockWidgetAddMapFeatures()
{
    delete ui;
}

void DockWidgetAddMapFeatures::fillList()
{
    QDir dir("map_items");
    if (!dir.exists()) return;
    QStringList types; types << "*.png" << "*.svg" << "*.jpg"<<".bmp"<<".tiff";
    QStringList files = dir.entryList(types, QDir::Files | QDir::NoDot | QDir::NoDotDot | QDir::Readable );

    foreach (QString file, files) {
        QListWidgetItem *item = new QListWidgetItem(QIcon(dir.path()+"\\"+file),file);
        ui->listWidget->addItem(item);
    }
}


void DockWidgetAddMapFeatures::on_pushButtonAddItem_clicked()
{
    if (ui->listWidget->selectedItems().size()<=0) return;

    QListWidgetItem * listItem = ui->listWidget->selectedItems()[0];

    if (listItem->text().contains(".svg",Qt::CaseInsensitive)){
        GraphicsFeatureItemSVG * item = new GraphicsFeatureItemSVG(this,"map_items\\"+listItem->text(), 1,1);
        getMapView()->getScene()->addItem(item);
        items->push_back(item);
    } else if (listItem->text().contains(".png",Qt::CaseInsensitive)){
        GraphicsFeatureItem * item = new GraphicsFeatureItem(this,"map_items\\"+listItem->text(), 1,1);
        getMapView()->getScene()->addItem(item);
        items->push_back(item);
    }
    getActionsGroup()->actions()[2]->toggle();

    return;
}

void DockWidgetAddMapFeatures::changeScale(qreal scale)
{
    for (int i =0; i<items->size();i++){
        QGraphicsItem * item = items->value(i);
        item->setScale(item->scale()*scale);
    }
}

void DockWidgetAddMapFeatures::resizeEvent(QResizeEvent *)
{
    ui->listWidget->setUpdatesEnabled(true);
    ui->listWidget->updateGeometry();
    ui->listWidget->sortItems();
}


void DockWidgetAddMapFeatures::on_toolButtonClearAll_clicked()
{
    for (int i =0; i<items->size();i++){
        QGraphicsItem * item = items->value(i);
        item->~QGraphicsItem();
    }
    items->clear();
}


void DockWidgetAddMapFeatures::on_toolButtonSetVisible_toggled(bool checked)
{
    for (int i =0; i<items->size();i++){
        QGraphicsItem * item = items->value(i);
        item->setVisible(checked);
    }
}

void DockWidgetAddMapFeatures::on_listWidget_doubleClicked(const QModelIndex &index)
{
    Q_UNUSED(index)
    on_pushButtonAddItem_clicked();
}


void DockWidgetAddMapFeatures::on_toolButtonSelectAll_clicked()
{
    for (int i =0; i<items->size();i++){
        QGraphicsItem * item = items->value(i);
        item->setSelected(true);
    }
}


void DockWidgetAddMapFeatures::on_toolButtonScaleMinus_clicked()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()) {
        if (items->contains(item)){
            item->setTransformOriginPoint(item->boundingRect().center());
            double scale = item->scale()-ui->doubleSpinBoxOpacity->value();
            if (scale>0)  item->setScale(scale);
        }
    }
}

void DockWidgetAddMapFeatures::on_toolButtonScalePlus_clicked()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()) {
        if (items->contains(item)){
            item->setTransformOriginPoint(item->boundingRect().center());
            double scale = item->scale()+ui->doubleSpinBoxOpacity->value();
            if (scale>0)  item->setScale(scale);
        }
    }
}

void DockWidgetAddMapFeatures::on_toolButtonOpacityMinus_clicked()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()) {
        if (items->contains(item)){
            item->setTransformOriginPoint(item->boundingRect().center());
            double opacity = item->opacity()-ui->doubleSpinBoxOpacity->value();
            if (opacity>0)  item->setOpacity(opacity);
        }
    }
}

void DockWidgetAddMapFeatures::on_toolButtonOpacityPlus_clicked()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()) {
        if (items->contains(item)){
            item->setTransformOriginPoint(item->boundingRect().center());
            double opacity = item->opacity()+ui->doubleSpinBoxOpacity->value();
            if (opacity>0)  item->setOpacity(opacity);
        }
    }
}

void DockWidgetAddMapFeatures::on_toolButtonSelectedItemsDelete_clicked()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()) {
        if (items->contains(item)){
            items->removeOne(item);
            delete item;
        }
    }
}

void DockWidgetAddMapFeatures::on_toolButtonSelectedItemsUnSelect_clicked()
{
    foreach (QGraphicsItem * item, getMapView()->getScene()->selectedItems()) {
        if (items->contains(item)){
            item->setSelected(false);
        }
    }
}

void DockWidgetAddMapFeatures::on_toolButton_clicked()
{
    QString dir = QFileDialog::getOpenFileName(this, tr("Add image file"),
                                               "/home",
                                               tr("Portable Network Graphics (*.png);;"
                                                  "Joint Photographic Experts Group (*.jpg);;"
                                                  "Tagged Image File Format (*.tiff);;"
                                                  "Windows Bitmap (*.bmp);;"
                                                  "Scalable Vector Graphics (*.svg)"
                                                  ));

    if (dir=="") return;

    QFile imgFile(dir);
    QString name = imgFile.fileName().right(imgFile.fileName().length()-imgFile.fileName().lastIndexOf("/")-1);


    QDir dir2;


    if (imgFile.copy(imgFile.fileName(),dir2.currentPath()+"/map_items/"+name)){
        ui->listWidget->clear();
        fillList();
    } else {
        QMessageBox msgBox;
        msgBox.setText(tr("Image was not loaded!"));
        msgBox.exec();
    }


}
